import time
import sys

# 定义要导入的模块列表及其导入方式
modules = [
    # 标准库模块
    ('threading', 'import threading'),
    ('yaml', 'import yaml'),
    ('json', 'import json'),
    ('datetime', 'import datetime'),
    ('pathlib', 'import pathlib'),
    ('time', 'import time'),
    ('shutil', 'import shutil'),
    ('subprocess', 'import subprocess'),
    ('os', 'import os'),
    ('sys', 'import sys'),
    ('re', 'import re'),
    ('requests', 'import requests'),
    ('psutil', 'import psutil'),
    ('ctypes', 'import ctypes'),
    ('typing', 'import typing'),
    ('configparser', 'import configparser'),
    ('zipfile', 'import zipfile'),
    ('sqlite3', 'import sqlite3'),
    ('random', 'import random'),
    
    # Selenium相关导入
    ('selenium.webdriver.edge.webdriver', 'from selenium.webdriver.edge.webdriver import WebDriver'),
    ('selenium.webdriver.common.action_chains', 'from selenium.webdriver.common.action_chains import ActionChains'),
    ('selenium.webdriver.support.ui', 'from selenium.webdriver.support.ui import WebDriverWait'),
    ('selenium.webdriver.edge.service', 'from selenium.webdriver.edge.service import Service'),
    ('selenium.webdriver.edge.options', 'from selenium.webdriver.edge.options import Options'),
    ('selenium.webdriver.remote.webelement', 'from selenium.webdriver.remote.webelement import WebElement'),
    ('selenium.webdriver.common.keys', 'from selenium.webdriver.common.keys import Keys'),
    ('selenium.webdriver.common.by', 'from selenium.webdriver.common.by import By'),
    ('selenium.webdriver.support', 'from selenium.webdriver.support import expected_conditions'),
    ('selenium.common.exceptions', 'from selenium.common.exceptions import *'),
    
    # PySide6相关导入
    ('PySide6.QtWidgets', 'from PySide6.QtWidgets import (QWidget,QMainWindow,QDialog,QFileDialog,QMessageBox,QPushButton,QListView,QListWidget,QListWidgetItem,QLineEdit,QTextEdit,QLabel,QHeaderView,QInputDialog,QApplication)'),
    ('PySide6.QtCore1', 'from PySide6.QtCore import (Qt,Signal,Slot,QObject,QMetaObject,QThread,QDateTime,QThreadPool,QFile,QFileDevice,QFileInfo,QTimer,QJsonDocument,QCoreApplication)'),
    ('PySide6.QtCore2', 'from PySide6.QtCore import (QTextStream, QStringListModel, QModelIndex)'),
]

# 存储每个模块的导入时间
import_times = []

# 测量导入时间
total_start = time.time()

for name, import_stmt in modules:
    start_time = time.time()
    try:
        # 执行导入语句
        exec(import_stmt)
        end_time = time.time()
        duration = (end_time - start_time) * 1000  # 转换为毫秒
        import_times.append((name, duration))
        print(f"导入 {name} 成功，耗时: {duration:.2f} 毫秒")
    except ImportError as e:
        print(f"导入 {name} 失败: {e}")
    except Exception as e:
        print(f"导入 {name} 时发生错误: {e}")

total_end = time.time()
total_duration = (total_end - total_start) * 1000  # 转换为毫秒

# 按耗时排序
import_times.sort(key=lambda x: x[1], reverse=True)

# 输出结果
print("\n" + "="*50)
print(f"总导入时间: {total_duration:.2f} 毫秒")
print("="*50)
print("各模块导入时间（从长到短）:")
for name, duration in import_times:
    print(f"{name}: {duration:.2f} 毫秒")
